package com.goddess.yeecolor.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.goddess.yeecolor.Modle.Person;
import com.goddess.yeecolor.R;
import com.goddess.yeecolor.interfaceAll.OnClickListener;
import com.goddess.yeecolor.tools.ImageUtils;

import java.util.List;

/**
 * 活动fragment
 */
public class ExerciseFragmentSingeAdapter extends BaseRecyclerAdapter<ExerciseFragmentSingeAdapter.SimpleAdapterViewHolder> {

    private OnClickListener mOnClick;
    private List<Person> list;
    private Context mContxt;
    public ExerciseFragmentSingeAdapter(List<Person> list, Context context) {
        this.list = list;
        this.mContxt=context;
    }

    private String stop="";
    @Override
    public void onBindViewHolder(SimpleAdapterViewHolder holder, int position, boolean isItem) {
        Person person = list.get(position);
        holder.item_exercisefragment_title.setText(person.getTitle());
        holder.item_exercisefragment_city.setText(person.getHost_location());
//        holder.item_exercisefragment_place.setText(person.getPrice());
        holder.item_exercisefragment_pices.setText(person.getPrice());
        if (person.getState()==0){
            holder.item_exercisefragment_btn.setText("火热报名中");
            if (person.getStop()==0){
                stop="已报名:";
            }
            else if(person.getStop()==1){
                stop="";
            }
            holder.item_exercisefragment_process.setText(stop+person.getInfo());
            holder.item_exercisefragment_pices.setVisibility(View.VISIBLE);
        }
        else if (person.getState()==1){
            if (person.getStop()==0){
                stop="已报名:";
            }
            else if(person.getStop()==1){
                stop="";
            }
            holder.item_exercisefragment_process.setText(stop+person.getInfo());
            holder.item_exercisefragment_btn.setText("活动进程");
            holder.item_exercisefragment_btn.setTextColor(mContxt.getResources().getColor(R.color.red));
            holder.item_exercisefragment_btn.setBackground(mContxt.getResources().getDrawable(R.drawable.exercisefrg_btn1));
            holder.item_exercisefragment_pices.setVisibility(View.INVISIBLE);
            holder.item_exercisefragment_process.setText(person.getInfo());
        }
        else if (person.getState()==2){
            if (person.getStop()==0){
                stop="已报名:";
            }
            else if(person.getStop()==1){
                stop="";
            }
            holder.item_exercisefragment_process.setText(stop+person.getInfo());
            holder.item_exercisefragment_btn.setText("活动记录");
            holder.item_exercisefragment_btn.setTextColor(mContxt.getResources().getColor(R.color.purple));
            holder.item_exercisefragment_btn.setBackground(mContxt.getResources().getDrawable(R.drawable.exercisefrg_btn2));
            holder.item_exercisefragment_pices.setVisibility(View.INVISIBLE);
        }

        holder.item_exercisefragment_time.setText(person.getAdd_date());

        ImageUtils.getInstance().ImageStart(mContxt,person.getThumb(),holder.item_exercisefragment_imageview);


    }

    @Override
    public int getAdapterItemViewType(int position) {
        return 0;
    }

    @Override
    public int getAdapterItemCount() {
        return list.size();
    }

    @Override
    public SimpleAdapterViewHolder getViewHolder(View view) {
        return new SimpleAdapterViewHolder(view, false);
    }

    /**
     * 顶部添加数据
     * @param list
     */
    public void setData(List<Person> lists) {

        list.clear();
        list.addAll(lists);
        notifyDataSetChanged();
    }

    public void addData(List<Person>lists){
//        if (isClear){
//            list.clear();
//        }
        lists.addAll(list);
        list.removeAll(list);
        list.addAll(lists);
        notifyDataSetChanged();
    }

    @Override
    public SimpleAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View v = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.item_exercisefragmentsingeadapter, parent, false);
        SimpleAdapterViewHolder vh = new SimpleAdapterViewHolder(v, true);
        return vh;
    }

    public void insert(Person person, int position) {
        insert(list, person, position);
    }

    public void remove(int position) {
        remove(list, position);
    }

    public void clear() {
        clear(list);
    }

    public class SimpleAdapterViewHolder extends RecyclerView.ViewHolder {

        public ImageView item_exercisefragment_imageview;
        public  TextView item_exercisefragment_title;
        public TextView item_exercisefragment_city;
        public TextView item_exercisefragment_place;
        public TextView item_exercisefragment_process;
        public TextView item_exercisefragment_time;
        public TextView item_exercisefragment_pices;
        public TextView item_exercisefragment_btn;

        private LinearLayout recycler_view_test_item_person_view;
        public SimpleAdapterViewHolder(View itemView, boolean isItem) {
            super(itemView);
            if (isItem) {
                item_exercisefragment_imageview= (ImageView) itemView.findViewById(R.id.item_exercisefragment_imageview);
                item_exercisefragment_title= (TextView) itemView.findViewById(R.id.item_exercisefragment_title);
                item_exercisefragment_city= (TextView) itemView.findViewById(R.id.item_exercisefragment_city);
//                item_exercisefragment_place= (TextView) itemView.findViewById(R.id.item_exercisefragment_place);
                item_exercisefragment_process= (TextView) itemView.findViewById(R.id.item_exercisefragment_process);
                item_exercisefragment_time= (TextView) itemView.findViewById(R.id.item_exercisefragment_time);
                item_exercisefragment_pices= (TextView) itemView.findViewById(R.id.item_exercisefragment_pices);
                item_exercisefragment_btn= (TextView) itemView.findViewById(R.id.item_exercisefragment_btn);
                recycler_view_test_item_person_view= (LinearLayout) itemView.findViewById(R.id.recycler_view_test_item_person_view);

                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mOnClick.OnClick(view,getLayoutPosition());
                    }
                });
            }


        }
    }

    public Person getItem(int position) {
        if (position < list.size())
            return list.get(position);
        else
            return null;
    }



    public void setOnClickListener(OnClickListener onClickListener){
        this.mOnClick=onClickListener;
    }
}
